package bid.test;

import bid.dao.ProfessorDao;
import bid.dao.ProjectDao;
import bid.dao.impl.ProjectDaoImpl;
import bid.entity.Professor;
import bid.entity.Project;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
public class ExportWord {
    public static void main(String[] args) throws IOException, TemplateException {
        ProjectDao dao=new ProjectDaoImpl();
        Project project = dao.get(371L);
        List<Professor> lists = dao.findProfsByProject(371L);

        Map<String,Object> dataMap=new HashMap<>();
        dataMap.put("proj",project);
        dataMap.put("profs",lists);
        //

        Configuration configuration = new Configuration();

        //设置编码
        configuration.setDefaultEncoding("UTF-8");

        //ftl模板文件统一放至 com.lun.template 包下面
        configuration.setClassForTemplateLoading(ExportWord.class,"/bid/dao/template/");

        //获取模板
        Template template = configuration.getTemplate("采购评标专家库专家抽取登记表.xml");

        //输出文件
        File outFile = new File(UUID.randomUUID()+".docx");

//
//        //如果输出目标文件夹不存在，则创建
//        if (!outFile.getParentFile().exists()){
//            outFile.getParentFile().mkdirs();
//        }
        //
        //将模板和数据模型合并生成文件
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));

        //生成文件
        template.process(dataMap, out);

        //关闭流
        out.flush();
        out.close();
    }
}
